package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.BannerDTO;
import com.quanquan.management.request.BannerRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;
import utils.StringUtil;

/**
 * @author gongzhou
 * @title: ModuleService
 * @projectName SpringCloud
 * @description: TODO
 * @date 2019/3/289:33
 */
@Service
public class BannerService {

    @Value("${module-url.storeUrl}")
    private String storeUrl;

    public PageDTO<BannerDTO> list(BannerRequest requestBody) {
        String json = Request.postJson(storeUrl + "banner/query_list_manage", requestBody);
        PageDTO<BannerDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            return data;
        }
        return pageDTO;
    }

    public boolean delete(String id) {
        BannerRequest requestBody = new BannerRequest();
        requestBody.setId(id);
        String json = Request.postJson(storeUrl + "banner/remove_banner", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return resCode == 0;
    }

    public BannerDTO findById(String id) {
        BannerRequest requestBody = new BannerRequest();
        requestBody.setId(id);
        requestBody.setType(0);
        String json = Request.postJson(storeUrl + "banner/query_by_id", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        BannerDTO bannerDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            bannerDTO = data.toJavaObject(BannerDTO.class);
        }
        return bannerDTO;
    }

    public boolean add(BannerRequest requestBody) {
        String json = Request.postJson(storeUrl + "banner/add_banner", requestBody);
        if (StringUtil.isEmpty(json)) {
            return false;
        }
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return 0 == resCode;
    }

    public boolean update(BannerRequest requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "banner/update_banner", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean updateStatus(BannerRequest requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "banner/update_status", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 != resCode) {
            return false;
        }
        return true;
    }

}
